from abc import ABC, abstractmethod
from typing import Any, Dict, List
from datetime import datetime
from vnpy.trader.object import BarData

class StrategyTemplate(ABC):
    """策略模板"""
    
    def __init__(self, strategy_engine: Any, strategy_name: str):
        """构造函数"""
        self.strategy_engine = strategy_engine
        self.strategy_name = strategy_name
        
        # 策略参数
        self.parameters: Dict[str, Any] = {}
        # 策略变量
        self.variables: Dict[str, Any] = {}
        
        # 信号相关
        self.buy_signal = True
        self.sell_signal = False
        
    @abstractmethod
    def on_init(self):
        """初始化策略"""
        pass
        
    @abstractmethod
    def on_start(self):
        """启动策略"""
        pass
        
    @abstractmethod
    def on_stop(self):
        """停止策略"""
        pass
        
    @abstractmethod
    def on_bar(self, bar: BarData):
        """K线更新回调"""
        pass
        
    @abstractmethod
    def get_signal(self) -> Dict:
        """获取交易信号"""
        pass
        
    @abstractmethod
    def get_parameters(self) -> Dict:
        """获取策略参数"""
        pass
        
    @abstractmethod
    def set_parameters(self, params: Dict):
        """设置策略参数"""
        pass

class StrategyEngine:
    """策略引擎"""
    
    def __init__(self):
        self.strategies = {}  # name: strategy
        
    def add_strategy(self, name: str, strategy: StrategyTemplate):
        """添加策略"""
        self.strategies[name] = strategy
        
    def remove_strategy(self, name: str):
        """移除策略"""
        if name in self.strategies:
            del self.strategies[name]
            
    def get_signal(self, name: str) -> Dict:
        """获取策略信号"""
        if name in self.strategies:
            return self.strategies[name].get_signal()
        return {} 